bool areEquals(in const float n1, in const float n2) {
    return abs(n1 - n2) < 1.0E-5;
}

// Stores angle in radians needed to rotate from Z axis
// to vector v in counterclockwise order
void radiansFromVector(in const float3 v, out float angle) {
    const float len = length(v);
    if (areEquals(len, 0.0f)) {
        angle = 0.0f;
    } else {
        angle = atan2(-v.x, v.z);
    }
}

// Stores a rotation matrix about y axis that matches vector v
// orientation.
void rotationAroundY(in const float3 v, out float4x4 m) {
    float angle;
    radiansFromVector(v, angle);

    m[0][0] = cos(angle);
    m[0][1] = 0.0f;
    m[0][2] = sin(angle);
    m[0][3] = 0.0f;

    m[1][0] = 0.0f;
    m[1][1] = 1.0f;
    m[1][2] = 0.0f;
    m[1][3] = 0.0f;

    m[2][0] = -sin(angle);
    m[2][1] = 0.0f;
    m[2][2] = cos(angle);
    m[2][3] = 0.0f;

    m[3][0] = 0.0f;
    m[3][1] = 0.0f;
    m[3][2] = 0.0f;
    m[3][3] = 1.0f;
}
